//
//  ViewController.m
//  婚礼纪
//
//  Created by dllo on 15/5/12.
//  Copyright (c) 2015年 蓝鸥. All rights reserved.
//

#import "ViewController.h"
#import "ConnectModel.h"
#import "UIImageView+DownLoader.h"
#import "DressCell.h"
#import "DressModel.h"
#import "DressController.h"
#import "ImageView.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, ConnectModelDelegate, DressCellDelegate>

@property (nonatomic, retain) UITableView * table;

@end

@implementation ViewController

- (void)dealloc
{
    [_table release];
    [_array release];
    [super dealloc];
}

- (void)loadView
{
    [super loadView];
    self.array = [NSMutableArray array];
    self.navigationItem.title = @"婚纱列表";
    [self createTable];
    [self starteConnect];
    NSLog(@"123");
    NSLog(@"321");
}

- (void)starteConnect
{
    [ConnectModel connectUrlWith:@"http://hunliji.com/products.json?page=1&user_id=862599&category=1&sort=0" delegate:self];
}

- (void)sendData:(NSData *)data
{
    NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    self.array = [dic objectForKey:@"products"];
    NSLog(@"aaa");
    [_table reloadData];
}

- (void)createTable
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _table.rowHeight = 300;
    _table.dataSource = self;
    _table.delegate = self;
    [self.view addSubview:_table];
    [_table release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return (self.array.count / 2) + (self.array.count % 2);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentify = @"cell";
    DressCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[DressCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify];
    }
    NSDictionary * dic1 = [NSDictionary dictionary];
    NSDictionary * dic2 = [NSDictionary dictionary];

    dic1 = [_array objectAtIndex:indexPath.row * 2];
    dic2 = [_array objectAtIndex:indexPath.row * 2 + 1];

    DressModel * model1 = [[DressModel alloc] init];
    [model1 setValuesForKeysWithDictionary:dic1];
    DressModel * model2 = [[DressModel alloc] init];
    [model2 setValuesForKeysWithDictionary:dic2];
    
    [cell.imageView1 setImageWithUrl:model1.photo_path placeHolder:nil];
    [cell.imageView2 setImageWithUrl:model2.photo_path placeHolder:nil];
    cell.imageView1.model = model1;
    cell.imageView2.model = model2;
    cell.delegate = self;
    cell.price1.text = [NSString stringWithFormat:@"💰%@", model1.price];
    cell.price2.text = [NSString stringWithFormat:@"💰%@", model2.price];
    
    cell.like_count1.text = [NSString stringWithFormat:@"❤️%@", model1.like_count];
    cell.like_count2.text = [NSString stringWithFormat:@"❤️%@", model2.like_count];
    

    
    return cell;
}


- (void)touchAction:(ImageView *)imageView
{
    DressController * second = [[DressController alloc] init];
    second.Id = imageView.model.Id;
    [self.navigationController pushViewController:second animated:YES];
    [second release];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
